package com.sk.bilibili.view.activity;

import static com.sk.bilibili.utils.StringUtils.str;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ms.square.android.expandabletextview.ExpandableTextView;
import com.previewlibrary.GPreviewActivity;
import com.sk.api.annotations.Autowired;
import com.sk.api.tool.BindTool;
import com.sk.bilibili.R;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.bean.Note;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.FocusMapper;
import com.sk.bilibili.mapper.PraiseMapper;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.utils.TimeUtils;
import com.sk.bilibili.utils.ViewUtils;
import com.sk.bilibili.view.diy.AvatarView;

public class ImageActivity extends GPreviewActivity {

    @Override
    public int setContentLayout() {
        return R.layout.activity_image;
    }

    @Autowired
    FocusMapper focusMapper;
    @Autowired
    PraiseMapper praiseMapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BindTool.getInstance().bind(this);
        note = (Note) getIntent().getSerializableExtra("note");
        member = (Member) getIntent().getSerializableExtra("member");
        bindView();
        initView_();
        bindEvent();
    }

    private void initView_() {
        avatar.setRes(member.getAvatar(), member.getExtra().getPendant());
        nickName.setText(member.getNickName());
        time.setText(TimeUtils.getShotTime(note.getCreateDate()));
        show.setText(note.getContent());
        ViewUtils.handleFocus(focus, member);
        forward.setText(str(note.getForwardNum(),"转发"));
        discuss.setText(str(note.getDiscussNum(),"评论"));
        praise.setText(str(note.getPraiseNum(),"点赞"));
    }

    private void bindEvent() {
        findViewById(R.id.iv_back).setOnClickListener(v -> transformOut());
        findViewById(R.id.iv_more).setOnClickListener(v->{

        });
        avatar.setOnClickListener(v->MemberActivity.start(this,member.getId()));
        nickName.setOnClickListener(v->MemberActivity.start(this,member.getId()));
        focus.setOnClickListener(v -> {
            RequestUtils.process(focusMapper.switchFocus(member.getId()),
                    focus -> toast(focus==null?"取消关注成功":"关注成功"));
            ViewUtils.switchFocus(focus,member);
        });
        forward.setOnClickListener(v->DynamicActivity.start(this,2,note.getId()));
        praise.setOnClickListener(v -> {
            ViewUtils.tint(praise,true);
            JsonMap map=new JsonMap();
            map.put("type",2);
            map.put("targetId",note.getId());
            RequestUtils.process(praiseMapper.switchPraise(map), aBoolean -> toast("操作成功"));
        });
    }

    private void toast(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    private Note note;
    private Member member;
    private AvatarView avatar;
    private TextView nickName;
    private TextView time;
    private ExpandableTextView show;
    private TextView focus;
    private TextView forward;
    private TextView discuss;
    private TextView praise;

    private void bindView() {
        avatar = findViewById(R.id.av_avatar);
        nickName = findViewById(R.id.tv_nike_name);
        time = findViewById(R.id.tv_time);
        show = findViewById(R.id.etv_desc);
        focus = findViewById(R.id.tv_focus);
        forward = findViewById(R.id.tv_forward);
        discuss = findViewById(R.id.tv_discuss);
        praise = findViewById(R.id.tv_praise);
    }

}